﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

using DragDraw.UserService;

namespace DragDraw.Util
{
    /// <summary>
    /// 全局错误管理器
    /// </summary>
    public class ErrorManager : IErrorServiceListener
    {
        private static ErrorManager m_instance = new ErrorManager();

        /// <summary>
        /// 获取ErrorManager的唯一实例
        /// </summary>
        public static ErrorManager Instance { get { return m_instance; } }

        private static bool IS_DEBUG = true;

        private ErrorManager() { }

        public void OnGetLastError(GetLastErrorCompletedEventArgs e)
        {
            HtmlPage.Window.Alert("服务器出现错误：\n" + e.Result);
        }

        /// <summary>
        /// 显示一条警告信息，触发OnWarnMessage
        /// </summary>
        /// <param name="message">信息内容</param>
        public void ShowWarnMessage(string message)
        {
            if (OnWarnMessage != null)
            {
                OnWarnMessage(this,
                    new ErrorMessageEventArgs() { Message = message });
            }
            else
            {
                HtmlPage.Window.Alert("警告：" + message);
            }
        }

        /// <summary>
        /// 显示一条调试信息，触发OnDebugMessage
        /// 设置IS_DEBUG = false将会使改方法失效
        /// </summary>
        /// <param name="message">信息内容</param>
        public void ShowDebugMessage(string message)
        {
            if (!IS_DEBUG) return;
            if (OnDebugMessage != null)
            {
                OnDebugMessage(this,
                    new ErrorMessageEventArgs() { Message = message });
            }
            else
            {
                HtmlPage.Window.Alert("Debug：" + message);
            }
        }

        /// <summary>
        /// 显示一条错误信息，触发OnErrorMessage
        /// </summary>
        /// <param name="message">信息内容</param>
        public void ShowErrorMessage(string message)
        {
            if (OnErrorMessage != null)
            {
                OnErrorMessage(this,
                    new ErrorMessageEventArgs() { Message = message });
            }
            else
            {
                HtmlPage.Window.Alert("错误：" + message);
            }
        }

        /// <summary>
        /// 显示异常，触发OnExceptionMessage
        /// </summary>
        /// <param name="e">信息内容</param>
        public void ShowException(Exception e)
        {
            int i = 0;
            Exception err = e;
            string msg = "出现异常：\n";
            while (err != null)
            {
                msg += "异常等级" + i.ToString() + " : " + err.Message + "\n";
                err = err.InnerException;
                i++;
            }
            HtmlPage.Window.Alert(msg);
        }

        /// <summary>
        /// 记录一段日志信息，会尝试显示在html页中id="log"的div中
        /// </summary>
        /// <param name="message">日志信息</param>
        /// <param name="heading">头部信息</param>
        public void LogMessage(string message, string heading)
        {
            if (!IS_DEBUG) return;
            HtmlElement elem;
            try
            {
                elem = HtmlPage.Document.GetElementById("log");
                elem.SetAttribute("innerText", elem.GetAttribute("innerText") +
                heading + " : " + message + "\n");
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 查看服务器出现的错误
        /// </summary>
        public void ShowLastServiceError()
        {
            ServiceManager.Instance.GetLastError(this);
        }

        /// <summary>
        /// 当遇到不可恢复的错误时，调用此函数触发OnShutdown结束系统
        /// 需要实现OnShutdown事件
        /// </summary>
        internal void SystemShutdown()
        {
            if (OnShutdown != null)
                OnShutdown(this, null);
        }

        public event EventHandler<ErrorMessageEventArgs> OnWarnMessage;
        public event EventHandler<ErrorMessageEventArgs> OnDebugMessage;
        public event EventHandler<ErrorMessageEventArgs> OnErrorMessage;
        public event EventHandler<ErrorMessageEventArgs> OnExceptionMessage;
        public event EventHandler<EventArgs> OnShutdown;
    }

    public class ErrorMessageEventArgs:EventArgs
    {
        public string Message;
    }
}
